package com.lemon.controller;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lemon.domain.*;
import com.lemon.service.WarehousingEntryDetailsService;
import com.lemon.service.WarehousingEntryDetailsSortService;
import com.lemon.service.WarehousingEntryService;
import com.lemon.service.impl.WarehousingEntryDetailsServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.List;

/**
* 入库明细表(warehousing_entry_details)表控制层
*
* @author xxxxx
*/
@RestController
@RequestMapping("/warehousing_entry_details")
@RequiredArgsConstructor
public class WarehousingEntryDetailsController {
/**
* 服务对象
*/
    private final WarehousingEntryDetailsService warehousingEntryDetailsService;

    private final WarehousingEntryService warehousingEntryService;

    private final WarehousingEntryDetailsSortService entryDetailsSortService;

    @GetMapping("/list")
    public Result getEntryList(){
        return Result.success(warehousingEntryDetailsService.list());
    }

    @GetMapping
    public Result getEntryDetailsPage(Integer page, Integer pageSize,Integer status){
        PageResult entryDetailsPage = warehousingEntryDetailsService.getEntryDetailsPage(page, pageSize, status);
        return Result.success(entryDetailsPage);
    }


    @PutMapping
    public Result updateEntryDetails(@RequestBody List<Integer> ids, @RequestParam Integer status) {
        // 检查参数是否有效
        if (ids == null || ids.isEmpty()) {
            return Result.error("请选择一个商品");
        }
        // 批量更新状态
        boolean success = true;
        for (Integer id : ids) {
            WarehousingEntryDetails details = warehousingEntryDetailsService.getById(id);
            if (details != null) {
                details.setStatus(status);
                details.setUpdateTime(LocalDateTime.now());
                if (status==2){
                    details.setRealArrivalTime(LocalDateTime.now());
                }
                if (!warehousingEntryDetailsService.updateById(details)) {
                    success = false;
                }
            }
        }
        if (status==2){
            ids.forEach(id -> {
                entryDetailsSortService.remove(Wrappers.<WarehousingEntryDetailsSort>lambdaQuery().eq(WarehousingEntryDetailsSort::getWarehousingEntryDetailsId,id));
            });
        }
        return success ? Result.success() : Result.error("部分更新失败");
    }



}
